<template>
	<view>
		<view class="antSign" v-if="isSignTwo==1">{{signtext}}</view>
		<view class="antSign two" v-if="isSignTwo==2">请勿离开，请勿操作，正在自动跳转中...</view>
		<view class="antSign three" v-if="isSignTwo==3">签约已完成! 请返回H5或App</view>
		<view class="antSign" v-if="isSignTwo==4">合同签约异常，请返回联系客服！</view>
	</view>
</template>
<script>
	const esignPlugin = requirePlugin("esign")
	import {
		APP_APPID
	} from '@/utils/constant';
	import {
		request
	} from '@/utils/index.js';
	export default {
		data() {
			return {
				signtext: '正在拉起合同准备签约...',
				isSignTwo: 1, //1:刚进来   2：e签宝合同完成     3：代扣签约完成    4：e签宝或者代扣签约异常
				antSignOrderId: '',
			};
		},
		onLoad(e) {
			// let urls =
			// `alipays://platformapi/startapp?appId=2021004145617675&page=pages/antSign1/antSign1&query= ${encodeURIComponent('orderId=MALL202408158099053280438517761944')}`
			// 	`alipays://platformapi/startapp?appId=2021004145617675&page=pages/index/index&query= ${encodeURIComponent('orderId=MALL202408158099053280438517761944')}`
			// console.log('模拟跳转到支付宝的链接', urls);
		},
		onShow() {

			const {
				code,
				message,
			} = esignPlugin.getSignStatusInfo();
			if (code) {
				this.isSignTwo = 2
				
				uni.showToast({
					title: '请稍等,请勿操作,正在跳转页面...',
					icon: 'none',
					duration: 2500
				});
				let timer1 = null
				timer1 = setTimeout(() => {
					uni.hideLoading()
					this.$onTo('/pages/antSign1/antSign1', false, true)
					clearTimeout(timer1);
					timer1 = null
				}, 2500); // 延迟2.5秒
				// this.$onTo('/pages/antSign1/antSign1', false, true)
			}


			// let antSignOrderId = uni.getStorageSync('antSignOrderId') || ''
			// if (antSignOrderId) {
			// 	this.antSignOrderId = antSignOrderId
			// 	console.log('之前存的订单id呢---', this.antSignOrderId);

			// 	const {
			// 		code,
			// 		message,
			// 	} = esignPlugin.getSignStatusInfo();
			// 	if (code) {
			// 		this.isSignTwo = 2
			// 		// this.getAntWithholdUrl() //获取支付宝代扣的参数
			// 		this.$onTo('/pages/antSign1/antSign1')
			// 	} else {
			// 		// this.isSignTwo = 4
			// 	}
			// }

		},
		methods: {
			getAntWithholdUrl() {
				this.$request(`order/mall/liteUserOrders/getOrderWithholdSign?orderId=${this.antSignOrderId}`, {}, 'get',
					false,
					true).then((res) => {
					if (res.businessSuccess || res.success) {
						this.antWithholdSigning(res.data.url, res.data.tenantCode, res.data.merchantld)
					} else {
						this.isSignTwo = 4
						uni.showToast({
							icon: 'none',
							title: res.errorMessage || res.msg || '签约异常，请联系客服处理！',
							duration: 3000
						});
					}
				});
			},
			antWithholdSigning(signStr, tenantId, merchantId) {
				my.navigateToMiniProgram({
					appId: "2021001152620480", // 固定值，无须修改。
					path: "pages/tripartite/index", // 固定值，无须修改。
					query: {
						signStr: signStr, // 代扣签约创建接口返回的sign_str内容
						tenantId: tenantId, // 商家的用户code，在数科控制台中查询获取
						merchantId: merchantId, // 商户id,也是企业社会信用代码，保持与进件接口一致
						orderId: this.antSignOrderId, // // 订单id，保持与进件接口一致
						redirectUrl: "alipays://platformapi/startapp?appId=2021004166678607&page=pages/antSign/antSign", // 代扣签署成功/失败回跳地址，示例 alipays://platformapi/startapp?appId=2021004108689505&page=pages/home/index，appId 和 page 需要根据实际情况调整真实数值
					}, // 需要携带的必要参数
					success: (res) => {
						if (res.resultStatus == '7000') {
							//代扣签约成功
							this.isSignTwo = 3
						}
					},
					fail: (error) => {
						if (res.resultStatus != '7000') {
							//代扣签约成功
							this.isSignTwo = 4
						}
					},
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.antSign {
		font-size: 34rpx;
		text-align: center;
		margin-top: 300rpx;
		letter-spacing: 0.05em;
		font-weight: 700;
	}

	.two {
		color: #ff1702;
	}

	.three {
		color: #38a9ff;
	}

	.antSign1 {
		font-size: 32rpx;
		text-align: center;
		margin-top: 300rpx;
		letter-spacing: 0.05em;
		background-color: #1677ff;
		color: #ffffff;
		padding: 10rpx 30rpx;
		border-radius: 30rpx;
		width: fit-content;
		margin-left: auto;
		margin-right: auto;

	}
</style>